//
//  TableViewController.m
//  Puzzle_Crazy
//
//  Created by Le Hung on 4/2/14.
//  Copyright (c) 2014 DanielLe. All rights reserved.
//

#import "TableViewController.h"
#import "DBManager.h"
#import "CategoryObject.h"
#import "AlbumsViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController
@synthesize arrData;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.view.backgroundColor = [UIColor clearColor];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(popBack)];
    [self.navigationItem setLeftBarButtonItem:backButton];
    
    arrData = [[NSMutableArray alloc] initWithArray:[DBManager shareInstance].showDataCategory];
    
    // Remove table cell separator
    [myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    // Assign our own backgroud for the view
    myTableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    
    // Add padding to the top of the table view
    UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
    myTableView.contentInset = inset;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

- (void)popBack
{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - DataSource UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  arrData.count;
}

- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rowCount = [self tableView:myTableView numberOfRowsInSection:0];
    NSInteger rowIndex = indexPath.row;
    UIImage *background = nil;
    
    if (rowIndex == 0) {
        background = [UIImage imageNamed:@"cell_top.png"];
    } else if (rowIndex == rowCount - 1) {
        background = [UIImage imageNamed:@"cell_bottom.png"];
    } else {
        background = [UIImage imageNamed:@"cell_middle.png"];
    }
    
    return background;
}

- (UIImage *)cellBackgroundForRowAtIndexPathSelected:(NSIndexPath *)indexPath
{
    NSInteger rowCount = [self tableView:myTableView numberOfRowsInSection:0];
    NSInteger rowIndex = indexPath.row;
    UIImage *background = nil;
    
    if (rowIndex == 0) {
        background = [UIImage imageNamed:@"cell_top_selected.png"];
    } else if (rowIndex == rowCount - 1) {
        background = [UIImage imageNamed:@"cell_bottom_selected.png"];
    } else {
        background = [UIImage imageNamed:@"cell_middle_selected.png"];
    }
    
    return background;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    UIView *selectedView = [[UIView alloc]init];
    selectedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_select.png"]];
    cell.selectedBackgroundView =  selectedView;
    UIButton *btn = (UIButton*)[cell viewWithTag:100];
    CategoryObject *cateObj = (CategoryObject*)[arrData objectAtIndex:indexPath.row];
    [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.jpg",cateObj.Objcategory,kRandom]] forState:UIControlStateNormal];
    
    // Assign our own background image for the cell
    UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
    
    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
    cellBackgroundView.image = background;
    cell.backgroundView = cellBackgroundView;
    
    // Assign our own selected background image for the cell
    UIImage *backgroundSelected = [self cellBackgroundForRowAtIndexPathSelected:indexPath];
    UIImageView *cellBackgroundViewSelected = [[UIImageView alloc] initWithImage:backgroundSelected];
    cellBackgroundViewSelected.image = background;
    cell.selectedBackgroundView = cellBackgroundViewSelected;
    
    return cell;
}

#pragma mark - Delegate UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    AlbumsViewController *album = (AlbumsViewController*)[storyboard instantiateViewControllerWithIdentifier:@"AlbumsViewController"];
    CategoryObject *cate = (CategoryObject*)[arrData objectAtIndex:indexPath.row];
    album.idPu = cate.Objid;
    [self presentViewController:album animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)back:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
